package dao;

import java.sql.*;

public class BaseDao {

	public Connection conn;
	public PreparedStatement ps;
	
	public ResultSet rs;
	
	
	public Connection getCon(){
		
		  try {
			
			  Class.forName("com.mysql.jdbc.Driver");
			  conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/bookstore","root", "ACCP");
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		return conn;
	}
	
	
	public void closeAll(ResultSet rs,PreparedStatement ps,Connection conn){
		try {
			if(rs!=null){
				rs.close();
			}
			
			if(ps!=null){
				ps.close();
			}
			
			if(conn!=null){
				conn.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	/**
	 * 增删改的通用方法
	 * @param sql
	 * @param param
	 * @return
	 * @throws Exception
	 */
	 public int zsg(String sql,Object...param) throws Exception{
		 
		   conn=getCon();
		    ps=conn.prepareStatement(sql);
		   if(param!=null){
			     for (int i = 0; i < param.length; i++) {
					 ps.setObject(i+1, param[i]);
				}
		   }
		 
		 
		 return ps.executeUpdate();
	 }
	
	
	
	
}
